package com.liangtian.mdm.base.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * 时间工具类
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";

    public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String formatFullTime(LocalDateTime localDateTime) {
        return formatFullTime(localDateTime, FULL_TIME_PATTERN);
    }

    public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(dateTimeFormatter);
    }

    private static String getDateFormat(Date date, String dateFormatType) {
        SimpleDateFormat simformat = new SimpleDateFormat(dateFormatType);
        return simformat.format(date);
    }

    public static String formatCSTTime(String date, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
        Date d = sdf.parse(date);
        return DateUtils.getDateFormat(d, format);
    }

    /**
     * @param date
     * @param partten 格式化类型 yyyy-MM-dd HH:mm:ss、yyyy-MM-dd hh:mm:ss、yyyy-MM-dd
     * @return 时间字符串
     * @desc 时间转字符串
     */
    public static String dateToString(Date date, String partten) {
        DateFormat format = new SimpleDateFormat(partten);
        if (null != date) {
            return format.format(date);
        } else {
            return "";
        }
    }

    /**
     * @param date    字符串时间
     * @param partten 格式化类型 yyyy-MM-dd HH:mm:ss、yyyy-MM-dd hh:mm:ss、yyyy-MM-dd
     * @return 时间
     * @desc 字符串转为时间格式
     */
    public static Date stringToDate(String date, String partten) {
        DateFormat format = new SimpleDateFormat(partten);
        Date d = null;
        try {
            d = format.parse(date);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return d;
    }
}
